<?php

class Paper extends DirModel
{
	

	public function getUpdateFields()
	{  
		return array(
			array(
				'name'=>'ut_format_id',
				'editor'=>'dropDown',
				'data'=>CActiveRecord::model('Format')->listData(),
			),
			array(
				'name'=>'ut_paper_kind_id',
				'editor'=>'dropDown',
				'data'=>CActiveRecord::model('PaperKind')->listData(),
			),
			array(
				'name'=>'ut_density_id',
				'editor'=>'dropDown',
				'data'=>CActiveRecord::model('Density')->listData(),
			),
			array(
				'name'=>'ut_paper_type_id',
				'editor'=>'dropDown',
				'data'=>CActiveRecord::model('PaperType')->listData(),
			),
			'ut_paper_price',									
		);
	}
	
	public $viewColumns=array(
		'format.ut_format_name',
		'paper_kind.ut_paper_kind_name',
		'density.ut_density_value',
		'paper_type.ut_paper_type_name',
		'ut_paper_price',
	);

	
	public function getPaperLabel()
	{
		return $this->paper_kind->ut_paper_kind_name.' '.$this->density->ut_density_value.' ('.$this->format->ut_format_name.' '.$this->paper_type->ut_paper_type_name.')';
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ut_paper';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ut_format_id, ut_paper_kind_id, ut_density_id, ut_paper_type_id, ut_paper_price', 'required'),
		//	array('ut_paper_price', 'numerical'),
			array('ut_format_id, ut_paper_kind_id, ut_density_id, ut_paper_type_id', 'type', 'type'=>'integer'),
			array('ut_paper_price', 'type', 'type'=>'float'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'density' => array(self::BELONGS_TO, 'Density', 'ut_density_id'),
			'format' => array(self::BELONGS_TO, 'Format', 'ut_format_id'),
			'paper_kind' => array(self::BELONGS_TO, 'PaperKind', 'ut_paper_kind_id'),
			'paper_type' => array(self::BELONGS_TO, 'PaperType', 'ut_paper_type_id'),
		);
	}

	
	public function defaultScope()
	{
		return array(
			'with'=>'density',
			'order'=>'ut_paper_kind_id,density.ut_density_value',
		);
	}
	
	public function scopes()
	{
		return array(
			'absence'=>array(
				'condition'=>'NOT EXISTS (select * from ut_press_paper upp WHERE t.ut_paper_id=upp.ut_paper_id AND upp.ut_press_id=:press)',
			),
		);
	}		
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ut_format_id' => 'Формат',
			'ut_paper_kind_id' => 'Бумага',
			'ut_density_id' => 'Плотность',
			'ut_paper_type_id' => 'Тип',
			'ut_paper_price' => 'Цена за кг(лист)',
		);
	}

}